package LoginTest;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-10-22
 * Time: 20:40
 */
@WebServlet("/index")
//这个类代表登录的主页-->显示主页的信息
public class IndexLogin extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf8");
        HttpSession session = req.getSession(false);
        if(session==null){
            //会话不存在(该用户不存在),返回登陆页
            resp.sendRedirect("login");
            return;
        }
        //否则这个用户已经登陆过了
        String userName = (String) session.getAttribute("userName");
        //获取访问次数
        Integer visitCount = (Integer) session.getAttribute("visitCount");
        session.setAttribute("visitCount",visitCount+=1);
        resp.getWriter().write("hi  " + userName +"! 登陆成功!"+"用户名为 : " + userName
        +"访问次数 : " + visitCount);
    }
}
